package Fruit;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 水果抽象类
 */
public abstract class Fruit {
    private String name;
    protected double unitPrice;
    private int weight;

    public Fruit(String name, Double unitPrice, Integer weight) {
        List<String> errors = new ArrayList<>();
        try{
            setName(name);
        } catch (Exception e) {
            errors.add(e.getMessage());
        }

        try {
            setUnitPrice(unitPrice);
        } catch (Exception e) {
            errors.add(e.getMessage());
        }

        try {
            setWeight(weight);
        } catch (Exception e) {
            errors.add(e.getMessage());
        }

        if (!errors.isEmpty()) throw new RuntimeException(String.join(",",errors));
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        if(Objects.isNull(name) || "".equals(name)) throw new RuntimeException("传入名字不能为空");
        else if(Objects.nonNull(this.name)) throw new RuntimeException("名字已设置，不能重复设置");
        this.name = name;
    }

    public Double getUnitPrice() {
        return unitPrice;
    }

    public void setUnitPrice(Double unitPrice) {
        if (unitPrice <= 0) throw new RuntimeException("单价不能小于等于0");

        this.unitPrice = unitPrice;
    }

    public Integer getWeight() {
        return weight;
    }

    public void setWeight(Integer weight) {
        if (weight <= 0) throw new RuntimeException("斤数不能小于等于0");

        this.weight = weight;
    }
}
